<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/02/28 12:37:57 $)
// $RCSfile: admin_settings.php,v $ : $Revision: 1.16 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
require_once ("./modules/core/functions_settings.php");
$template->loadLanguageSection("settings");

/**
* ###############################################
* Display the settings for a specific group
* ###############################################
*/
if ($_REQUEST["do"] == "settinglist" && trim($_REQUEST["groupid"]) != "")
{

	$dbCore->query("SELECT * FROM `".TABLE_PREFIX."settingsgroups` WHERE `sgroupid` = '". intval($_REQUEST["groupid"]) ."';");
	$dbCore->nextRecord();
	if ($dbCore->Record["sgroupid"] == $_REQUEST["groupid"] && trim($_REQUEST["groupid"]) != "")
	{
		$group = $dbCore->Record;

		if ($_REQUEST["step"] == "1")
		{
			$infomessage = sprintf($_SWIFT["language"]["settingsupdatedfor"], $_SWIFT["language"][$group["name"]]);

			// Empty the Cache Directory
			if ($handle = opendir('./cache')) {
				while (false !== ($file = readdir($handle))) { 
					if ($file != "." && $file != ".." && $file != "index.html") { 
						@unlink("./cache/".$file); 
					}
				}
				closedir($handle); 
			}

		}

		if ($_SWIFT["isdemo"] == true)
		{
			$infomessage = $_SWIFT["language"]["demomode"];
		}

		$template->assign("backurl", "index.php?_m=core&_a=settings");
		$interface->adminHeader($_SWIFT["language"]["settings"]);

		$interface->adminNavBar($_SWIFT["language"]["settings"], $_SWIFT["language"]["d_".$group["name"]], -1);

		printFormStart();
		printInfoBox($infomessage);

		printMainTableHeader(sprintf($_SWIFT["language"]["settingsfor"], $_SWIFT["language"][$group["name"]]));

		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."settingsfields` WHERE `sgroupid` = '". intval($group["sgroupid"]) ."' ORDER BY `displayorder` ASC;");
		while ($dbCore->nextRecord())
		{
			if ($_SWIFT["isdemo"] == true)
			{
				// Ignore
			} else if ($_REQUEST["step"] == "1") {
				// Time to update the setting

				if (is_array($_REQUEST[$dbCore->Record["name"]]))
				{
					$setarray = serialize($_REQUEST[$dbCore->Record["name"]]);

					$settings->updateKey("settings", $dbCore->Record["name"], $setarray);

					$_SWIFT["settings"][$dbCore->Record["name"]] = $_REQUEST[$dbCore->Record["name"]];
				} else {
					$settings->updateKey("settings", $dbCore->Record["name"], $_REQUEST[$dbCore->Record["name"]]);
					$_SWIFT["settings"][$dbCore->Record["name"]] = $_REQUEST[$dbCore->Record["name"]];
				}
			}
			
			if ($dbCore->Record["settingtype"] == "text" || $dbCore->Record["settingtype"] == "password")
			{
				$fieldvalue = $_SWIFT["settings"][$dbCore->Record["name"]];
				printTextRow($dbCore->Record["name"], $_SWIFT["language"][$dbCore->Record["name"]], $_SWIFT["language"]["d_".$dbCore->Record["name"]], $dbCore->Record["settingtype"], $fieldvalue);

			} else if ($dbCore->Record["settingtype"] == "yesno") {
				if ($_SWIFT["settings"][$dbCore->Record["name"]] == "1")
				{
					$fieldvalue = true;
				} else {
					$fieldvalue = false;
				}
				printYesNoRow($dbCore->Record["name"], $_SWIFT["language"][$dbCore->Record["name"]], $_SWIFT["language"]["d_".$dbCore->Record["name"]], $fieldvalue);
			} else if ($dbCore->Record["settingtype"] == "number") {
				$fieldvalue = $_SWIFT["settings"][$dbCore->Record["name"]];
				printTextRow($dbCore->Record["name"], $_SWIFT["language"][$dbCore->Record["name"]], $_SWIFT["language"]["d_".$dbCore->Record["name"]], "text", $fieldvalue, 15);
				
			} else if ($dbCore->Record["settingtype"] == "title") {
				printDescRow($_SWIFT["language"][$dbCore->Record["name"]]);
			} else if ($dbCore->Record["settingtype"] == "color") {
				$fieldvalue = $_SWIFT["settings"][$dbCore->Record["name"]];
				printColorRow($dbCore->Record["name"], $_SWIFT["language"][$dbCore->Record["name"]], $_SWIFT["language"]["d_".$dbCore->Record["name"]], $fieldvalue);
			} else if ($dbCore->Record["settingtype"] == "custom") {
				ob_start();
				eval("?>".$dbCore->Record["customvalue"]);
				// <?
				$contents = ob_get_contents();
				ob_end_clean();

				printDefaultDescRow($_SWIFT["language"][$dbCore->Record["name"]], $_SWIFT["language"]["d_".$dbCore->Record["name"]], $contents);
			} else if ($dbCore->Record["settingtype"] == "hidden") {
				$fieldvalue = $_SWIFT["settings"][$dbCore->Record["name"]];
				addHiddenField($dbCore->Record["name"], $fieldvalue);
			}
		}
		printSubmitRow($_SWIFT["language"]["updatesettings"]);

		printMainTableFooter();
		addHiddenField("_m", "core");
		addHiddenField("_a", "settings");
		addHiddenField("do", "settinglist");
		addHiddenField("groupid", $_REQUEST["groupid"]);
		addHiddenField("step", "1");
		printFormEnd();

		$interface->adminFooter();
		flush();

	}


/**
* ###############################################
* Display the default setting group list
* ###############################################
*/
} else {
	$interface->adminHeader($_SWIFT["language"]["settings"]);

	$interface->adminNavBar($_SWIFT["language"]["settings"], $_SWIFT["language"]["desc_settings"], -1);

	printInfoBox($infomessage);

	printMainTableHeader($_SWIFT["language"]["systemsettings"]);

	$colarray[0]["align"] = "left";
	$colarray[0]["value"] = $_SWIFT["language"]["groups"];
	$colarray[1]["width"] = "120";
	$colarray[1]["value"] = $_SWIFT["language"]["options"];
	$colarray[1]["nowrap"] = true;
	
	printDataRow($colarray, "tabletitlerow");


	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."settingsgroups` WHERE `ishidden` = '0' ORDER BY `displayorder` ASC;");
	while ($dbCore->nextRecord())
	{
		if (!$module->isRegistered($dbCore->Record["module"]))
		{
			continue;
		}

		$colarray = array();
		$colarray[0]["align"] = "left";
		$colarray[0]["value"] = '<img src="'. $_SWIFT["themepath"] .'icon_settings.gif" align="absmiddle" border="0" /> <a href="index.php?_m=core&_a=settings&do=settinglist&groupid='. urlencode($dbCore->Record["sgroupid"]) .'" title="'. $_SWIFT["language"]["edit"] .'">'. $_SWIFT["language"][$dbCore->Record["name"]] .'</a>';

		$colarray[1]["value"] = '<a href="index.php?_m=core&_a=settings&do=settinglist&groupid='. urlencode($dbCore->Record["sgroupid"]) .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'.$_SWIFT["language"]["edit"].'</a>';

		printDataRow($colarray);
	}


	printMainTableFooter();

	$interface->adminFooter();
}
?>